package vashion.azeroth.api.controller.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import vashion.azeroth.common.http.HttpContentGetter;
import vashion.azeroth.core.lenux.pojo.LenuxApiResult;
import vashion.azeroth.core.lenux.pojo.LenuxProduct;
import vashion.azeroth.core.manager.item.LenuxItemWriteManager;

public class UpdateLenuxInventoryController {

	private final static Logger logger = LoggerFactory.getLogger(UpdateLenuxInventoryController.class);

	private Boolean isOpen;

	private String url;// 应用地址

	private String appid;// 账号

	private String pageSize;// 每次获取商品数量
	
	private String lenuxSupplierName;// lenux在本系统中的账号
	
	private String timestampFormat;// 时间格式

	@Autowired
	private LenuxItemWriteManager lenuxItemWriteManager;

	public void updateLenuxInventory() {
		if (isOpen) {
			SimpleDateFormat sdf = new SimpleDateFormat(timestampFormat);
			String timestamp = sdf.format(new Date());
			Map<String, String> nameValueMap = Maps.<String, String>newHashMap();
			nameValueMap.put("appid", appid);
			nameValueMap.put("pageSize", pageSize);
			nameValueMap.put("timestamp", timestamp);
			List<LenuxProduct> allProductList = Lists.newArrayList();

			try {
				logger.info("同步新增货存开始");
				for (int page = 1; page < 2000; page++) {
					nameValueMap.put("page", String.valueOf(page));
					String returnMessage = HttpContentGetter.getContentString(url, nameValueMap, null);
					LenuxApiResult result = null;
					if (StringUtils.isNotEmpty(returnMessage)) {
						result = JSON.parseObject(returnMessage, LenuxApiResult.class);
						if (CollectionUtils.isEmpty(result.getData())) {
							break;
						} else {
							allProductList.addAll(result.getData());
						}
					}
				}
				for (LenuxProduct product : allProductList) {
					lenuxItemWriteManager.publishItem(product, lenuxSupplierName);
				}
				logger.info("同步新增货存结束");
			} catch (Exception e) {
				logger.error("同步新增货存错误：" + e.getMessage());
				e.printStackTrace();
			}
		}
	}

	public Boolean getIsOpen() {
		return isOpen;
	}

	public void setIsOpen(Boolean isOpen) {
		this.isOpen = isOpen;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getLenuxSupplierName() {
		return lenuxSupplierName;
	}

	public void setLenuxSupplierName(String lenuxSupplierName) {
		this.lenuxSupplierName = lenuxSupplierName;
	}

	public String getTimestampFormat() {
		return timestampFormat;
	}

	public void setTimestampFormat(String timestampFormat) {
		this.timestampFormat = timestampFormat;
	}
	
}
